/**
 * ZHU framework. Copyright 2012-, all rights reserved.
 *
 * $Id: ExcelOutputServiceImpl.java 31 2012-08-29 07:24:32Z zhuxiaopeng $
 * $Revision: 31 $
 * $Date: 2012-08-29 15:24:32 +0800 (三, 2012-08-29) $
 * $Author: zhuxiaopeng $
 */
package daily.service;

import java.io.*;
import java.util.*;

import javax.ejb.*;

import org.apache.poi.hssf.usermodel.*;

import daily.entity.*;
import daily.util.*;

/**
 * <p>
 * </p>
 * 
 * @author $Author: zhuxiaopeng $
 * @version $Revision: 31 $ - $Date: 2012-08-29 15:24:32 +0800 (三, 2012-08-29) $
 */
@Stateless
public class ExcelOutputServiceImpl {

	public HSSFWorkbook createWorkBook(final List<Staff> staffList, final InputStream in) throws IOException {
		final HSSFWorkbook book = new HSSFWorkbook(in);
		for (int i = 1; i < staffList.size(); i++) {
			book.cloneSheet(0);
		}
		final Date date = new Date();
		final Date startDate = DateUtils.getFirstDayOfWeek(date);
		final Date endDate = DateUtils.getLastDayOfWeek(date);
		int index = 0;
		final Map<Staff, List<DailyData>> dailyMap = dailyService.getDailyByPeriod(staffList, startDate, endDate);
		for (final Staff staff : staffList) {
			final List<DailyData> dailyList = dailyMap.get(staff);
			final HSSFSheet sheet = book.getSheetAt(index);
			book.setSheetName(index, staff.getName());
			sheet.getRow(0).getCell(1).setCellValue(staff.getName());
			int rowIndex = 1;
			for (final DailyData daily : dailyList) {
				final HSSFRow row = sheet.getRow(rowIndex++);
				row.getCell(0).setCellValue(daily.getDaily().getDate());
				row.getCell(1).setCellValue(daily.getDaily().getContent());
			}
			index++;
		}
		book.setForceFormulaRecalculation(true);
		return book;
	}

	public HSSFWorkbook createMonthWorkBook(final List<Staff> staffList, final InputStream in, final Date date) throws IOException {
		final HSSFWorkbook book = new HSSFWorkbook(in);
		{
			final HSSFSheet baseSheet = book.getSheetAt(0);
			final int maxDays = DateUtils.getDayOfMonth(date);
			removeRows(baseSheet, 33 - (31 - maxDays), 33);
		}

		for (int i = 1; i < staffList.size(); i++) {
			book.cloneSheet(0);
		}
		final Date startDate = DateUtils.getFirstDayOfMonth(date);
		final Date endDate = DateUtils.getLastDayOfMonth(date);
		int index = 0;
		final Map<Staff, List<DailyData>> dailyMap = dailyService.getDailyByPeriod(staffList, startDate, endDate);
		for (final Staff staff : staffList) {
			final List<DailyData> dailyList = dailyMap.get(staff);
			final HSSFSheet sheet = book.getSheetAt(index);
			book.setSheetName(index, staff.getFormalName());
			sheet.getRow(0).getCell(0).setCellValue(startDate);
			sheet.getRow(1).getCell(12).setCellValue(staff.getFormalName());
			int rowIndex = 2;
			for (final DailyData daily : dailyList) {
				final HSSFRow row = sheet.getRow(rowIndex++);
				if (daily.getDaily().getContent() != null) {
					row.getCell(2).setCellValue(daily.getDaily().getStartTime());
					row.getCell(3).setCellValue(daily.getDaily().getEndTime());
					row.getCell(6).setCellValue(daily.getDaily().getContent());
				}
			}
			index++;
		}
		book.setForceFormulaRecalculation(true);
		return book;
	}

	private void removeRows(final HSSFSheet baseSheet, final int begin, final int end) {
		for (int i = 0; i < end - begin; i++) {
			baseSheet.shiftRows(end - i, baseSheet.getLastRowNum(), -1);
		}
	}

	@EJB
	private DailyServiceImpl dailyService;
}
